import com.sun.source.tree.Tree;

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root = null;
    public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < key){
                cur = cur.right;
            }else if (cur.val > key){
                cur = cur.left;
            }else {
                return true;
            }
        }
        return false;
    }

    public  void checkInsertLegal() throws insertException{
        throw new insertException("待插入的值在树中已存在");
    }
    public void insert(int val){
        TreeNode node = new TreeNode(val);
        if (root == null) {
            root = node;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        //寻找插入位置
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < val){
                parent = cur;
                cur = cur.right;
            }else {
                try {
                    checkInsertLegal();
                }
                catch (insertException e){
                    e.printStackTrace();
                }
                return;
            }
        }
        //插入
        if (parent.val > val) {
            parent.left = node;
        }
        else{
            parent.right = node;
        }
    }
    public void remove(int key) {
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < key) {
                cur = cur.right;
            } else if (cur.val > key) {
                cur = cur.left;
            } else {
                //删除(三种情况)
                //removeNode();
            }
        }
    }
    private void removeNode(TreeNode parent,TreeNode cur) {
        if(cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //取左树最大值替代
                TreeNode target = cur.right;
                TreeNode targetP = cur;
                while(cur.left != null){
                    targetP = target;
                    target = target.left;
                }
            cur.val = target.val;
                if (target == targetP) {
                    targetP.left = target.right;
                }else {
                    targetP.right = target.right;
                }
        }
    }

}
